#!/bin/sh
# -- Script begins here --

INFO=$1
if [ "$INFO" = "none" ]; then
  INFO=zone
fi
ZONE=$2
if [ "$ZONE" = "form-zone" ]; then
  ZONE=$FORM_zone
fi
SERVER=$3
if [ "$SERVER" = "none" ] || [ "$SERVER" = "" ]; then
  SERVER=127.0.0.1
fi
DOMAIN=$4
if [ "$ZONE" = "" ]; then
  ZONE=$DOMAIN
fi

FindInfo ()
{
  local NAME=$1
  local FILE=$SIM_NAMEDMONDIR/names/$NAME
  if [ -f "$FILE" ]; then
    LINES=`wc -l $FILE | awk '{ print $1 }'`
    FDATE=`ls -l $FILE | awk '{ print $6 " " $7 " " $8 }'`
    echo "<A HREF=$SIM_NAMEDMONURL?named+name+$NAME>$LINES / $FDATE</A>"
  else
    echo "-"
#   echo "$FILE"
  fi
}

DisplayServers ()
{
  grep "SERVER:" $SIPMANDHCPSERVERLIST | while read V1 NAME IPADDR REST
  do
    echo ". <A HREF=$SCRIPT_NAME?named+$INFO+$ZONE+$IPADDR>$IPADDR</A> ."
  done
  echo "<BR>"
}

DisplayZones ()
{
  local TMPFILE=$SIM_TMPDIR/sortzones.$$
# echo "Getting zones from $SIM_NAMEDCONF"
  echo "<FORM method=post Action=$SCRIPT_NAME?named+$INFO+form-zone+$SERVER>
        <select name=zone size=1>"
  # sort the zones, dns domains first, in-addr later.
  cat $SIM_NAMEDZONES/* | while read DNSZONE
  do
    echo "<OPTION VALUE=$DNSZONE>$DNSZONE</option>"
  done
  echo "</SELECT>
        <INPUT TYPE=submit Value=\"Query\">
        </FORM>"
  rm $TMPFILE
}

DisplayDnsClients ()
{
NAME=$1
if [ -f $SIM_NAMEDMONDIR/names/$NAME ]; then
  TMPFILE=$SIM_TMPDIR/Sipmon-DisplayDnsClients-$$
  echo "<TR> <TD></TD><TD COLSPAN=5 BGCOLOR=#EEEEEE> <TABLE>"
  CLCOUNT=1
  OLDCLIENT=""
  sort -k 2 $SIM_NAMEDMONDIR/names/$NAME > $TMPFILE
  echo "- end -" >> $TMPFILE
  cat $TMPFILE | while read DATE CLIENT PORT NQUERY
  do
    if [ "$OLDCLIENT" = "$CLIENT" ]; then
      COUNT=`expr $COUNT + 1`
      OLDDATE=$DATE
    else
      if [ "$OLDCLIENT" != "" ]; then
        DNQUERY=`$SIM_HOST $OLDCLIENT | awk '{ print $5 }'`
        if [ "$DNQUERY" = "3(NXDOMAIN)" ]; then
          DNQUERY="-"
        fi
        echo "<TR><TD>$CLCOUNT</TD>
                  <TD><A HREF=$SIM_NAMEDMONURL?client+$OLDCLIENT>$OLDCLIENT</A></TD>
                  <TD>$DNQUERY</TD>
                  <TD ALIGN=RIGHT>$COUNT</TD>
                  <TD>$OLDDATE</TD></TR>"
        CLCOUNT=`expr $CLCOUNT + 1`
      fi
      COUNT=1
      OLDDATE=$DATE
      OLDCLIENT=$CLIENT
    fi
  done
  rm $TMPFILE
  echo "</TABLE> </TD> </TR>"
fi
}
readFile ()
{
  FILE=$1
  if [ -f $FILE ]; then
    cat $FILE
  else
    printf "-"
  fi
}

DispIpStatusColor ()
{
  NIPADDR=$1
  NNETDIR=$SIM_IPDATAB/`echo $NIPADDR | sed 's#\.#/#g'`
  NSTATUS=`readFile $NNETDIR/pollstatus.txt`
  case ${NSTATUS:20} in
    (${NMAPNAME[1]})  STATUSBGCOLOR=${NMAPCOLOR[1]} ; TYPE=E ;;
    (${NMAPNAME[2]})  STATUSBGCOLOR=${NMAPCOLOR[2]} ; TYPE=E ;;
    (${NMAPNAME[3]})  STATUSBGCOLOR=${NMAPCOLOR[3]} ; TYPE=E ;;
    (${NMAPNAME[4]})  STATUSBGCOLOR=${NMAPCOLOR[4]} ; TYPE=E ;;
    (${NMAPNAME[5]})  STATUSBGCOLOR=${NMAPCOLOR[5]} ; TYPE=E ;;
    (${NMAPNAME[6]})  STATUSBGCOLOR=${NMAPCOLOR[6]} ; TYPE=E ;;
#   (${NMAPNAME[7]})  STATUSBGCOLOR=${NMAPCOLOR[7]} ; TYPE=E ;;
    (*)               STATUSBGCOLOR=${NMAPCOLOR[0]} ; TYPE=U ;;
  esac
  echo $STATUSBGCOLOR
}

GenerateNamedReport ()
{
  if [ "$ZONE" != "" ]; then
    TMPFILE=$SIM_TMPDIR/host-zone-$ZONE-$$
    $SIM_HOST -a -l $ZONE $SERVER | grep "$ZONE" > $TMPFILE
    echo "<TABLE>
          <TR><TD COLSPAN=4 ALIGN=center>Num reccords for zone: $ZONE `cat $TMPFILE | wc -l`</TD></TR>
          <TR BGCOLOR=#DDDDDD><TD></TD><TD>Name</TD><TD>TYPE</TD><TD>Used / Queried</TD></TR>
         "
    LINECOUNT=1
    cat $TMPFILE | while read QUERY TTL IN DNSTYPE ANSWER
    do
      FQUERY=`echo $QUERY | tr '[A-Z]' '[a-z]'`
      case $DNSTYPE in
      (A)
        DNSINFO=`FindInfo $QUERY`
        HOSTNAME=`$SIM_HOST $ANSWER | awk '{ print $5 }'`
        #if [ "$QUERY" = "$HOSTNAME" ]; then
        if [ "$FQUERY" = "$HOSTNAME" ]; then
          RECTYPE=PRI
          BGCOLOR=#90EE90
          HOSTNAME="-"
        else
          RECTYPE=A
          BGCOLOR=#ADD8E6
        fi
        echo "<TR BGCOLOR=$BGCOLOR><TD BGCOLOR=`DispIpStatusColor $ANSWER`>$LINECOUNT</TD><TD>$QUERY</TD><TD>$RECTYPE
              <A HREF=$SCRIPT_NAME?mon+nodestatus+$ANSWER>$ANSWER</A>
              ($HOSTNAME)</TD><TD>$DNSINFO</TD>
              </TR>"
        if [ "$INFO" = "report" ]; then
          DisplayDnsClients $QUERY
        fi
        LINECOUNT=`expr $LINECOUNT + 1`
      ;;
      (CNAME|NS)
        case $DNSTYPE in
          (CNAME)  BGCOLOR=#F5DEB3 
	           DNSINFO=`FindInfo $QUERY` ;;
          (NS)     BGCOLOR=#FF99FF
	           DNSINFO="" ;;
        esac
        echo "<TR BGCOLOR=$BGCOLOR><TD>$LINECOUNT</TD><TD>$QUERY</TD><TD>$DNSTYPE $ANSWER"
        $SIM_HOST $ANSWER | while read H1 H2 H3 H4 REST
        do
          if [ "$H4" = "found:" ]; then
            echo "$REST"
          else
            echo "<A HREF=$SCRIPT_NAME?mon+nodestatus+$H4>$H4</A>"
          fi
        done
        echo "</TD><TD>$DNSINFO</TD>
              </TR>"
       # if [ "$INFO" = "report" ] && [ "$DNSTYPE" = "CNAME" ]; then
        if [ "$INFO" = "report" ] && [ "$DNSTYPE" = "CNAME" ]; then
          DisplayDnsClients $QUERY
        fi
        LINECOUNT=`expr $LINECOUNT + 1`
      ;;
      (SOA)
        echo "<TR BGCOLOR=#FFFF99>
              <TD>$LINECOUNT</TD>
              <TD>$QUERY</TD>
              <TD COLSPAN=2>SOA $ANSWER</TD></TR>"
        LINECOUNT=`expr $LINECOUNT + 1`
      ;;
      (MX)
        echo "<TR BGCOLOR=#FF9999>
              <TD>$LINECOUNT</TD>
              <TD>$QUERY</TD>
              <TD COLSPAN=2>MX $ANSWER"
        $SIM_HOST `echo $ANSWER | awk '{ print $2 }'` | while read H1 H2 H3 H4 REST
        do
          echo "<A HREF=$SCRIPT_NAME?mon+nodestatus+$H4>$H4</A>"
        done
        echo "</TD></TR>"
        LINECOUNT=`expr $LINECOUNT + 1`
      ;;
      (HINFO)
# QUERY TTL IN DNSTYPE ANSWER 
        echo "<TR BGCOLOR=#BBBBFF>
              <TD>$LINECOUNT</TD>
              <TD>$QUERY</TD>
              <TD COLSPAN=2>HINFO $ANSWER"
        $SIM_HOST $QUERY | while read H1 H2 H3 H4 REST
        do
          echo "<A HREF=$SCRIPT_NAME?mon+nodestatus+$H4>$H4</A>"
        done
        echo "</TD></TR>"
        LINECOUNT=`expr $LINECOUNT + 1`
      ;;
      (PTR)
        IPQUERY=`echo $QUERY | awk -F. '{ print $4 "." $3 "." $2 "." $1}'` 
        echo "<TR BGCOLOR=#BBBBBB>
              <TD>$LINECOUNT</TD>
              <TD>$QUERY</TD>
              <TD COLSPAN=2>PTR $ANSWER"
        $SIM_HOST $ANSWER | while read H1 H2 H3 H4 REST
        do
          #echo "H4=$H4, IPQUERY=$IPQUERY "
          if [ $H4 = $IPQUERY ]; then
            TEXTCOLOR=black
          else
            TEXTCOLOR=red
          fi 
          echo "<A HREF=$SCRIPT_NAME?mon+nodestatus+$H4><FONT COLOR=$TEXTCOLOR>$H4</FONT></A>"
        done
        echo "</TD></TR>"
        LINECOUNT=`expr $LINECOUNT + 1`
      ;;
      (*)
        if [ "$IN" = "IN" ]; then
          echo "<TR><TD>$LINECOUNT</TD><TD>$QUERY</TD><TD>$DNSTYPE $ANSWER</TD><TD></TD></TR>"
          LINECOUNT=`expr $LINECOUNT + 1`
        fi
      ;;
      esac
      if [ "$LINECOUNT" -gt "10000" ]; then
        echo "<TR><TD></TD><TD COLSPAN=2>Maximum reached, list aborted.</TD>"
        break
      fi
    done
    echo "</TABLE>"
  else
    echo "<H4>No domain selected.....</H4>"
  fi

}

displayNameInfo ()
{
  local NAME=$1
  TMPFILE=$SIM_TMPDIR/displayname-$NAME-$$
  echo "<TABLE>"
  LINECOUNT=1
  OLDCLIENT=""
  sort -k 2 $SIM_NAMEDMONDIR/names/$NAME > $TMPFILE
  echo "- end -" >> $TMPFILE
  cat $TMPFILE | while read DATE CLIENT PORT NSNAME
  do
    if [ "$OLDCLIENT" = "$CLIENT" ]; then
      COUNT=`expr $COUNT + 1`
      OLDDATE=$DATE
    else
      if [ "$OLDCLIENT" != "" ]; then
        DNSNAME=`$SIM_HOST $OLDCLIENT | awk '{ print $5 }'`
        if [ "$DNSNAME" = "3(NXDOMAIN)" ]; then
          DNSNAME="-"
        fi
        echo "<TR><TD>$LINECOUNT</TD><TD>$OLDDATE</TD>
                  <TD><A HREF=$SCRIPT_NAME?named+client+$OLDCLIENT>$OLDCLIENT</A></TD>
                  <TD>$DNSNAME</TD>
                  <TD>$COUNT</TD>
                  <TD>$NAME</TD></TR>"
        LINECOUNT=`expr $LINECOUNT + 1`
      fi
      COUNT=1
      OLDDATE=$DATE
      OLDCLIENT=$CLIENT
    fi
  done
  rm $TMPFILE
}

displayClientInfo ()
{
  local NAME=$1
  DNSNAME=`$SIM_HOST $NAME | awk '{ print $5 }'`
  if [ "$DNSNAME" = "3(NXDOMAIN)" ]; then
    DNSNAME="-"
  fi
  TMPFILE=$SIM_TMPDIR/displayclient-$NAME-$$
  echo "<TABLE>"
  LINECOUNT=1
  OLDNSNAME=""
  sort -k 3 $SIM_NAMEDMONDIR/clients/$NAME > $TMPFILE
  echo "- end -" >> $TMPFILE
# echo "<PRE>`cat $TMPFILE`</PRE>"
  cat $TMPFILE | while read DATE CLIENT NSNAME
  do
    if [ "$OLDNSNAME" = "$NSNAME" ]; then
      COUNT=`expr $COUNT + 1`
      OLDDATE=$DATE
    else
      if [ "$OLDNSNAME" != "" ]; then
        echo "<TR><TD>$LINECOUNT</TD><TD>$OLDDATE</TD>
        <TD>$NAME</TD>
        <TD>$DNSNAME</TD>
        <TD><A HREF=$SCRIPT_NAME?named+name+$OLDNSNAME.>$OLDNSNAME</A></TD>
        <TD>$COUNT</TD>
        <TD>$NAME</TD></TR>"
        LINECOUNT=`expr $LINECOUNT + 1`
      fi
      COUNT=1
      OLDDATE=$DATE
      OLDNSNAME=$NSNAME
    fi
  done
  rm $TMPFILE
}
# -------------------------- Main ---------------------------

echo "
<A HREF=$SCRIPT_NAME?named+zone+$ZONE+$SERVER>zone</A>
- <A HREF=$SCRIPT_NAME?named+mon+$ZONE+$SERVER>dnsmonitor</A>
- <A HREF=$SCRIPT_NAME?named+report+$ZONE+$SERVER>dnsreport</A>
- <A HREF=$SCRIPT_NAME?named+zonecount+all_zones+none>zone count</A>
- <A HREF=$SCRIPT_NAME?named>main</A>
</TD></TR><TR><TD COLSPAN=3><CENTER>
Servers:
`DisplayServers`
`DisplayZones`
</TD></TR></TABLE><CENTER>
<HR>

<H1>DNS zone information tool for: $ZONE</H1>
"
if [ "$INFO" = "zone" ]; then
  if [ "$ZONE" != "none" ]; then
    echo "<B>Number of reccords found: `$SIM_HOST -l $ZONE $SERVER | grep "$ZONE" | wc -l`.</B>"
#   $SIM_HOST -l $ZONE $SERVER
#   $SIM_HOST -l $ZONE $SERVER | grep "$ZONE"
    echo "<TABLE>"
    $SIM_HOST -a -l $ZONE $SERVER | while read QUERY TTL IN RECT ANSWER
    do
      if [ "$IN" = "IN" ]; then
        echo "<TR><TD>$QUERY</TD><TD>$TTL</TD><TD>$IN</TD><TD>$RECT</TD><TD>$ANSWER</TD></TR>"
      else
        echo "<TR><TD COLSPAN=5 BGCOLOR=#DDDDDD><PRE>$QUERY $TTL $IN $RECT $ANSWER</PRE></TD></TR>"
      fi
    done
    echo "</TABLE>"
  else
    echo "<H4>No domain selected.....</H4>"
  fi
elif [ "$INFO" = "mon" ]; then
  GenerateNamedReport $INFO $ZONE 
elif [ "$INFO" = "report" ]; then
  if [ -f $SIM_TMPDIR/namedreport-$ZONE ]; then
    cat $SIM_TMPDIR/namedreport-$ZONE
  else 
    if [ -f $SIM_TMPDIR/namedreport-$ZONE* ]; then
      echo "Generation of report for zone $ZONE already in progress.<BR>
            `ls -l $SIM_TMPDIR/namedreport-$ZONE* | awk '{ print "Size: "$5 ", at: " $6 " " $7 " " $8 }'`<BR>"
    else
      GENTMPFILE=$SIM_TMPDIR/namedreport-$ZONE.$REMOTE_USER.$$
      echo "Generating a namedreport for $ZONE<BR>"
      GenerateNamedReport $INFO $ZONE | tee $GENTMPFILE
      mv $GENTMPFILE $SIM_TMPDIR/namedreport-$ZONE
    fi
  fi
elif [ "$INFO" = "zonecount" ]; then
  COUNT=1
  echo "<TABLE>
        <TR><TD></TD><TD>Zone</TD>"
  grep "SERVER:" $SIPMANDHCPSERVERLIST | while read V1 NAME IPADDR REST
  do
    echo "<TD ALIGN=RIGHT>$IPADDR</TD>"
  done
  echo "</TR>"

  cat $SIM_NAMEDCONF | grep zone | grep type | grep -v in-addr | sort | sed 's/\"//g' | while read TYPE DNSZONE REST
  do
    LASTSOANUM=""
    echo "<TR><TD ALIGN=RIGHT>$COUNT</TD><TD>$DNSZONE</TD>"
    grep "SERVER:" $SIPMANDHCPSERVERLIST | while read V1 NAME IPADDR REST
    do
      SOANUM=`$SIM_HOST -l -t soa $DNSZONE $IPADDR | tail -n 1 | awk '{ print $5 }'`
      if [ "$SOANUM" = "$LASTSOANUM" ] || [ "$LASTSOANUM" = "" ]; then
        FONTCOLOR=black
      else
        FONTCOLOR=Crimson
      fi
      if [ "$SOANUM" != "" ] && [ "$LASTSOANUM" = "" ]; then
        LASTSOANUM=$SOANUM
      fi

      echo "<TD ALIGN=CENTER>
            <A HREF=$SCRIPT_NAME?named+zone+$DNSZONE+$IPADDR TARGET=_blank>
            <FONT COLOR=$FONTCOLOR>"
#     echo "$DNSZONE $IPADDR"
      $SIM_HOST -W 2 -a -l $DNSZONE $IPADDR | grep "$DNSZONE" | grep -v AXFR | wc -l
      echo "<BR>$SOANUM</FONT></A>
            </TD>"
    done
    echo "</TR>"
    COUNT=`expr $COUNT + 1`
  done
  cat $SIM_NAMEDCONF | grep zone | grep type | grep in-addr | sed 's/\"//g' | while read TYPE DNSZONE REST
  do
    echo "<TR><TD ALIGN=RIGHT>$COUNT</TD><TD>$DNSZONE</TD>"
    grep "SERVER:" $SIPMANDHCPSERVERLIST | while read V1 NAME IPADDR REST
    do
      echo "<TD ALIGN=CENTER>
      <A HREF=$SCRIPT_NAME?named+zone+$DNSZONE+$IPADDR TARGET=_blank>"
#     echo "$DNSZONE $IPADDR"
      $SIM_HOST -W 2 -l $DNSZONE $IPADDR | grep "$DNSZONE" | wc -l
      echo "</A>
            </TD>"
    done
    echo "</TR>"
    COUNT=`expr $COUNT + 1`
  done

  echo "</TABLE>"
elif [ "$INFO" = "name" ]; then
  displayNameInfo $ZONE
elif [ "$INFO" = "client" ]; then
  displayClientInfo $ZONE
else
  echo "<H4>This tool will provide info for mirored dns domains at Sipman<P>No domain selected.....</H4>"
fi

